///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2009. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

package
{

import by.blooddy.core.data.DataBase;
import by.blooddy.core.data.DataContainer;
import by.blooddy.core.events.data.DataBaseEvent;
import by.blooddy.core.utils.time.getTimer;

import com.bit101.components.Label;
import com.bit101.components.PushButton;

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.system.System;

import ru.goodcore.utils.ActivityListener;
	

/**
 *  @author				Dimarik
 *  @version			1.1
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="400", height="300", frameRate="21", backgroundColor="#E0EEEE")]

public class DataTreeTest extends Sprite {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function DataTreeTest() {
		super();
		super.addEventListener(Event.ADDED_TO_STAGE, this.initialize);
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 */
	private var _dataBase:			DataBase;

	/**
	 * @private 
	 */
	private var _sampleList:		Array = new Array();
	
	private var _textField:			Label;

	/**
	 * @private 
	 */
	private var _exit:				Boolean = true;
	
	private var _numObjects:		uint = 5e4;

	private var _nestedLevel:		uint = 3;

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function initialize(event:Event):void {
		(event.target as IEventDispatcher).removeEventListener(Event.ADDED_TO_STAGE, this.initialize);
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;
		var activityListener:ActivityListener = new ActivityListener();
		activityListener.x = 5;
		activityListener.y = 5;
		super.stage.addChild(activityListener);

		var button:PushButton = new PushButton(this, 270, 10, 'play', this.handler_pressPlay);

		this._textField = new Label(this, 270, 40);
		this._textField.text = 'numObjects: ' + this._numObjects + ', nestedLevel: ' + this._nestedLevel;
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	private function startTest():void {
		var numObjects:		uint = this._numObjects;
		var nestedLevel:	uint = this._nestedLevel;
		var loop:			uint = numObjects / nestedLevel;

		var parent:	DataContainer;
		this._dataBase = new DataBase();
		this._dataBase.addEventListener(DataBaseEvent.ADDED, this.handler_added);

		System.gc();

		var dc:			DataContainer;
		var startTime:	int = getTimer();

		for (var i:uint = 0; i < loop; i++) {

			parent = this._dataBase;

			for (var j:uint = 0; j < nestedLevel; j++) {
				parent = parent.addChild(new DataContainer()) as DataContainer;
				//parent = parent.addChildAt(new DataContainer(), 0) as DataContainer;
			}

		}

		var duration:int = getTimer() - startTime;
		this.calculateAverage(duration);
	}

	private function calculateAverage(duration:int):void {
		this._sampleList.push(duration);

		var average:	int;

		this._sampleList.forEach(
			function (element:*, index:int, arr:Array):void {
				average += int(element);
			}
		);
		var memory:Number = System.totalMemory / 1024;
		trace('duration', duration, 'average', int(average / this._sampleList.length), 'memory', memory);
		this._textField.text = 'average: ' + int(average / this._sampleList.length) + ', memory: ' + memory;
		
		
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	private function handler_added(event:Event):void {
		//trace('saas', event);
		event;
	}

	/**
	 * @private
	 */	
	private function handler_pressPlay(event:Event):void {
		this.startTest();
		
		//return;
		this._exit = !this._exit;

		if (this._exit) {
			super.removeEventListener(Event.ENTER_FRAME, this.handler_enterFrame);
		} else {
			super.addEventListener(Event.ENTER_FRAME, this.handler_enterFrame);
		}

	}

	/**
	 * @private
	 */	
	private function handler_enterFrame(event:Event):void {
		if ( !this._exit )
			this.startTest();
	}


}
}